﻿using System;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace BackupObjects
{
    /// <summary>
    /// Map n to m rel. between backup obj. and tag., used to persist in the repository.
    /// </summary>
    public sealed class BackupObjectTagRow : TableStorageEntity, IIdentifiableEntity
    {
        /// <summary>
        /// Gets or sets the backup object id.
        /// </summary>
        public Guid BackupObjectId { get; set; }

        /// <summary>
        /// Gets or sets the tag id.
        /// </summary>
        public Guid TagId { get; set; }

        public override sealed string PartitionKey
        {
            get
            {
                return base.PartitionKey;
            }
            set
            {
                base.PartitionKey = value;
            }
        }


        public override string RowKey
        {
            get { return Id.ToString(); }
            set
            {
                Id = Guid.Parse(value);
            }
        }

        public Guid Id { get; set; }

        /// <summary>
        /// The name of partitionName;
        /// </summary>
        private const string PartitionName = "BackupObjectTag";

        public BackupObjectTagRow()
        {
            PartitionKey = PartitionName;
        }

        public static implicit operator BackupObjectTag(BackupObjectTagRow backupObjectTagRow)
        {
            if (backupObjectTagRow == null)
            {
                return null;
            }

            return new BackupObjectTag()
                       {
                           BackupObjectId = backupObjectTagRow.BackupObjectId,
                           TagId = backupObjectTagRow.TagId,
                           Id = backupObjectTagRow.Id
                       };
        }

        public static implicit operator BackupObjectTagRow(BackupObjectTag backupObjectTag)
        {
            if (backupObjectTag == null)
            {
                return null;
            }

            return new BackupObjectTagRow()
            {
                Id=backupObjectTag.Id,
                BackupObjectId = backupObjectTag.BackupObjectId,
                TagId = backupObjectTag.TagId
            };
        }
    }
}
